package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;

import sjtu.ist.warehouse4ws.model.CustDomainmodelattr;
import sjtu.ist.warehouse4ws.model.CustDomainmodelstate;
import sjtu.ist.warehouse4ws.model.DomainEpc;
import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Domainmodelattr;
import sjtu.ist.warehouse4ws.model.Domainmodelstate;
import sjtu.ist.warehouse4ws.service.DomainModelAttributeService;
import sjtu.ist.warehouse4ws.service.DomainModelService;
import sjtu.ist.warehouse4ws.service.DomainModelStateService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainModelAttributeService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainModelService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainModelStateService;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ShowDomainModelAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3559225084848274205L;
	private DomainModelAttributeService domainModelAttributeService;
	private CustDomainModelAttributeService custDomainModelAttributeService;
	private DomainModelStateService domainModelStateService;
	private CustDomainModelStateService custDomainModelStateService;
	private DomainModelService domainModelService; 
	private List<Domainmodel> domainModelList = new ArrayList<Domainmodel> ();
	private List<Domainmodelattr> attributeList = new ArrayList<Domainmodelattr> ();
    private List<Domainmodelstate> stateList=new ArrayList<Domainmodelstate>();
   // private List<CustDomainmodelattr> custAttributeList = new ArrayList<CustDomainmodelattr>();
   // private List<CustDomainmodelstate> custStateList = new ArrayList<CustDomainmodelstate>();
    private String domainId;


	public String getDomainId() {
		return domainId;
	}
	public void setDomainId(String domainId) {
		this.domainId = domainId;
	}
	public void setDomainModelAttributeService(
		DomainModelAttributeService domainModelAttributeService) {
		this.domainModelAttributeService = domainModelAttributeService;
	}
	public void setDomainModelStateService(
		DomainModelStateService domainModelStateService) {
		this.domainModelStateService = domainModelStateService;
	}
	public void setDomainModelService(DomainModelService domainModelService) {
		this.domainModelService = domainModelService;
	}
	public void setCustDomainModelAttributeService(
			CustDomainModelAttributeService custDomainModelAttributeService) {
		this.custDomainModelAttributeService = custDomainModelAttributeService;
	}
	public void setCustDomainModelStateService(
			CustDomainModelStateService custDomainModelStateService) {
		this.custDomainModelStateService = custDomainModelStateService;
	}
	public void setCustDomainModelService(
			CustDomainModelService custDomainModelService) {
	}
	//DomainModel
	public String showDomainModelAttribute(){
		HttpServletRequest request = ServletActionContext.getRequest ();
		this.domainId=request.getParameter("cid");
	//	Domainmodel dm=domainModelService.getDomainModel(domainId);
		Subject currentUser = SecurityUtils.getSubject();		
		Integer user_id=(Integer) currentUser.getSession().getAttribute("userId");
		
		if(!currentUser.hasRole("superadmin"))
		{
			List<CustDomainmodelattr> templist=custDomainModelAttributeService.getDomainModelAttributeList(domainId);
			attributeList=new ArrayList<Domainmodelattr>();
			for(CustDomainmodelattr d:templist)
			{
				attributeList.add( d);
			}
		}
		else
		{
			attributeList=domainModelAttributeService.getDomainModelAttributeList(domainId);
		}
		
		
		return SUCCESS;
		}
	public String showDomainModelState(){
		Subject currentUser = SecurityUtils.getSubject();		
		HttpServletRequest request = ServletActionContext.getRequest ();
		this.domainId=request.getParameter("cid");
		
		if(!currentUser.hasRole("superadmin"))
		{
			List<CustDomainmodelstate> templist=custDomainModelStateService.getCustDomainModelStateList(domainId);
			this.stateList = new ArrayList<Domainmodelstate>();
			for(CustDomainmodelstate d : templist){
					stateList.add( d);
			}
		}
		else
		{
			stateList=domainModelStateService.getDomainModelStateList(domainId);
		}
		
		return SUCCESS;
		}
	@JSON(name = "totalAttribute")	
	public int getTotalAttribute() {
		return this.attributeList.size();
	}
    
	@JSON(name ="Domainmodelattr")	
	public List<Domainmodelattr> getAttributeList() {
		return this.attributeList;
	}
	@JSON(name = "totalState")	
	public int getTotalState() {
		return this.stateList.size();
	}
    
	@JSON(name ="Domainmodelstate")	
	public List<Domainmodelstate> getStateList() {
		return this.stateList;
	}
	
	public String showDomainModel(){
		
		HttpServletRequest request = ServletActionContext.getRequest ();
		this.domainId=request.getParameter("cid");
	    domainModelList = domainModelService.getDomainmodelListById(domainId);
	    return SUCCESS;
	}

	@JSON(name = "totalDomainModel")	
	public int getTotalDomainModel() {
		return this.domainModelList.size();
	}
    
	@JSON(name ="DomainModel")	
	public List<Domainmodel> getDomainModelList() {
		return this.domainModelList;
	}
	
	
}
